<script setup lang="ts">
import screen from "/@/assets/Test1.jpeg";
import {useUserStore, useWebSocketStore} from "/@/store";
import {storeToRefs} from "pinia";

const userStore = useUserStore();
console.log("用户信息")
console.log(userStore.uid)
const webSocketStore = useWebSocketStore();
const message = ref<string>("")
const {
    currentRoomId,
    currentMemberList,
    currentContact,
    currentAllMessageList
} = storeToRefs(webSocketStore);
import {getAssetsImge} from '/@/utils/getImge'
// import socketUtils from "/@/utils/websocket"
import {ElMessage} from "element-plus";
import {WebSocketReqMsgContentType} from "/@/utils/types";
import {MessageDto, MessageService} from "/@/api/openapi";
import {buildTextMsg} from "/@/utils/message"
import {Search} from "@element-plus/icons-vue";

const send = ref<Boolean>(true)
// 搜素键盘回车事件
const search = (e: any) => {
    console.log(e.keyCode)
    if (e.keyCode == 13 && send.value) {
        sendMessage()
    }
}
// 开启键盘监听事件
window.addEventListener('keydown', search, true)

const sendMessage = () => {
    const tmp = message.value;
    if (tmp === "" || message.value === undefined) {
        // ElMessage({
        //     type: 'error',
        //     message: "文字为空",
        // })
        return;
    }
    message.value = ""
    send.value = false;
    console.log("发送新消息了")
    // 目前还没有做回复消息的
    const msg: MessageDto = {
        roomId: currentRoomId.value,
        msgType: 1,
        body: buildTextMsg(tmp, undefined, undefined)
    }
    MessageService.sendMsg(msg).then((res) => {
        if (res.code !== 200 || res.data === undefined) {
            ElMessage({
                type: 'error',
                message: res.message,
            })
            return;

        }
        // 放数据
        webSocketStore.updateMessage({
            pageSize: 10,
            pageNum: 1,
            roomId: currentRoomId.value
        })

    }, (error) => {
        ElMessage({
            type: 'error',
            message: error,
        })
    }).finally(() => {
        send.value = true;
    })

}

const gridData = reactive([
    {
        date: '2016-05-02',
        name: 'Jack',
        address: 'New York City',
    },
    {
        date: '2016-05-04',
        name: 'Jack',
        address: 'New York City',
    },
    {
        date: '2016-05-01',
        name: 'Jack',
        address: 'New York City',
    },
    {
        date: '2016-05-03',
        name: 'Jack',
        address: 'New York City',
    },
])
</script>

<template>
    <el-container class="container">
        <el-header height="60px">
            <div class="card-header">
                <span>{{ currentContact.name }}</span>
                <!--                <el-button class="button" text>Operation button</el-button>-->
                <el-image class="image" :src="getAssetsImge('guanli.svg')"></el-image>
            </div>
        </el-header>
        <el-main class="el-main">
            <div v-for="(item,index) in currentAllMessageList" :key="index" class="message-avatar-content-container">
                <div v-if="item.fromUid!==userStore.uid" class="box-left">
                    <el-popover placement="right" :width="250" trigger="hover">
                        <template #reference>
                            <div class="avator-outer">
                                <el-avatar shape="square" class="avator" :size="50" :src="item.avatar"></el-avatar>
                            </div>
                        </template>
                        <div class="externInformation">
                            <div class="extern_top">
                                <div class="extern_avator_outer">
                                    <el-image class="extern_avator" shape="square" :src="item.avatar"></el-image>
                                </div>
                                <div class="extern_left">
                                    <span>昵称:{{ item.username }}</span>
                                    <span>uid:{{ item.fromUid }}</span>
                                    <span>其他信息:</span>
                                </div>
                            </div>
                            <div class="extern_bottom">
                                <div class="bottom_item">
                                    <el-image size="20" class="image" :src="getAssetsImge('tianjiahaoyou.svg')"/>
                                    <span>添加好友</span>
                                </div>
                                <div class="bottom_item">
                                    <el-image size="20" class="image" :src="getAssetsImge('xiaoxi.svg')"/>
                                    <span>发送消息</span>
                                </div>
                            </div>
                        </div>
                    </el-popover>
                    <div class="center">
                        <div class="name">
                            {{ item.username }}
                        </div>
                        <div class="text-message-container">
                            <p> {{ item.content }}</p>
                            <i></i>
                        </div>
                    </div>
                </div>
                <div v-else class="box-right">
                    <div class="center">
                        <div class="name">
                            {{ item.username }}
                        </div>
                        <div class="text-message-container">
                            <p>{{ item.content }}</p>
                            <i></i>
                        </div>
                    </div>
                    <div class="avator-outer">
                        <el-avatar class="avator" shape="square" :size="50" :src="item.avatar"></el-avatar>
                    </div>
                </div>
            </div>
        </el-main>
        <el-footer height="180px">
            <div class="input-action-container">
                <ul class="">
                    <li>
                        <el-image size="20" class="image" :src="getAssetsImge('emoji.svg')"/>
                    </li>
                    <li>
                        <el-image size="20" class="image" :src="getAssetsImge('file.svg')"/>
                    </li>
                    <li>
                        <el-image size="20" class="image" :src="getAssetsImge('shiping.svg')"/>
                    </li>
                    <li>
                        <el-image size="20" class="image" :src="getAssetsImge('yuyin.svg')"/>
                    </li>

                </ul>
                <ul class="">
                    <li>
                        <el-image size="20" class="image" :src="getAssetsImge('dianhua.svg')"/>
                    </li>
                    <li>
                        <el-image size="20" class="image" :src="getAssetsImge('video.svg')"/>
                    </li>
                </ul>
            </div>
            <div class="box-bottom">
                <el-input class="box-input"
                          type="textarea"
                          maxlength="500"
                          show-word-limit
                          placeholder="Enter发送，Ctrl+Enter换行"
                          @keyup.enter.native="search"
                          v-model="message">
                    <template #append>
                        <el-button :icon="Search"/>
                    </template>
                </el-input>
            </div>
        </el-footer>
    </el-container>
</template>

<style scoped lang="scss">
$height-variable: 27px;

.externInformation {
  display: flex;
  flex-direction: column;
  height: 130px;
  width: 100%;

  .extern_top {
    display: flex;
    height: 70px;
    margin-bottom: 10px;
    .extern_avator_outer {
      height: 70px;
      width: 70px;
      display: flex;
      align-items: center;

      .extern_avator {
        height: 50px;
        width: 50px;
        margin: 0 auto;

      }
    }

    .extern_left {
      display: flex;
      font-size: 14px;
      flex-direction: column;
      justify-content: center; /* 垂直居中 */
      align-items: flex-start; /* 水平居中 */
    }
  }

  .extern_bottom {
    display: flex;

    .bottom_item {
      display: flex;
      flex-direction: column;
      margin-right: 10px;
      justify-content: center; /* 垂直居中 */
      align-items: center; /* 水平居中 */
      .image {
        height: 25px;
        width: 25px;
      }
    }
  }
}

.container {
  padding: 0;
  border-right: 1px solid #e5e5e5;

  .el-header {
    padding: 0;

    .card-header {
      height: 60px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 10px 0 20px;
      background-color: #f5f5f5;
      border-bottom: 1px solid #e6e6e6;
      border-top-right-radius: 10px;
      position: relative;

      .image {
        height: 30px;
        width: 30px;
        //margin: 0 auto;
      }
    }
  }

  .el-main {
    padding: 0 20px 0 15px;
    overflow: auto;
    height: calc(100vh - 360px);

    .message-avatar-content-container {
      //display: flex;
      //max-width: calc(100% - 60px);
      //align-items: flex-start;
      //overflow: hidden;
      //text-overflow: ellipsis;

      .box-left {
        width: 100%;
        display: flex;
        margin-bottom: 10px;


        .avator {
          height: 50px;
          width: 50px;
          //margin: 0 auto;
        }

        .avator-outer {
          display: flex;
          justify-content: center;
          align-items: center;
        }

        .center {
          margin-left: 10px;

          .name {
            padding-left: 10px;
            height: 30px;
            line-height: 30px;
            vertical-align: center;
          }

          .text-message-container {
            i {
              position: absolute;
              left: -9px;
              border: solid transparent;
              //border-color: transparent;
              border-right-color: white;
              border-width: 5px;
            }

            margin: 0 10px;
            padding: 10px;
            background-color: #fff;
            position: relative;
            border-radius: 5px;
            display: flex;
            align-items: center;

            p {
              color: #050505;
              font-size: 13px;
              line-height: 20px;
              max-width: 400px;
              word-spacing: normal;
              word-break: break-word;
              overflow: hidden;
              display: inline-block;
              text-overflow: ellipsis;
              white-space: pre-line;
            }
          }
        }
      }

      .box-right {
        width: 100%;
        display: flex;
        justify-content: flex-end; /* 这会使所有子元素靠右排列 */
        align-items: flex-end;
        margin-bottom: 10px;

        .avator {
          height: 50px;
          width: 50px;
          //margin: 0 auto;
        }

        .avator-outer {
          display: flex;
          justify-content: center;
          align-items: center;
        }

        .center {
          margin-left: 10px;

          .name {
            height: 30px;
            line-height: 30px;
            vertical-align: center;
            text-align: right;
            padding-right: 10px;
          }

          .text-message-container {

            i {
              position: absolute;
              left: 100%;
              border: solid transparent;
              //border-color: transparent;
              border-left-color: #a8bdff;
              border-width: 5px;
            }

            margin: 0 10px;
            padding: 10px;
            background-color: #a8bdff;
            position: relative;
            border-radius: 5px;
            display: flex;
            align-items: center;

            p {
              color: #050505;
              font-size: 13px;
              line-height: 20px;
              max-width: 400px;
              word-spacing: normal;
              word-break: break-word;
              overflow: hidden;
              display: inline-block;
              text-overflow: ellipsis;
              white-space: pre-line;
            }
          }
        }
      }
    }
  }

  .el-footer {
    border-top: 1px solid #e5e5e5;
    min-height: 180px;
    //padding-top: 20px;
    //上 右 下 左
    padding: 10px 10px 0 10px;
    //padding: ;
    .input-action-container {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 10px 0 10px 0;

      ul {
        list-style: none;
        display: flex;

        li {
          flex: 1;
          margin-right: 10px;
          height: $height-variable;
        }
      }

      .image {
        width: $height-variable;
        height: $height-variable;
        margin: 0 auto;
      }
    }

    .box-bottom {
      height: 110px;

      .box-input {
        height: 110px;
      }
    }
  }
}

</style>